package com.r2coding.test;

import com.alibaba.fastjson2.JSON;
import com.r2coding.domain.strategy.model.entity.RaffleAwardEntity;
import com.r2coding.domain.strategy.model.entity.RaffleFactorEntity;
import com.r2coding.domain.strategy.model.entity.RuleActionEntity;
import com.r2coding.domain.strategy.service.armory.IStrategyArmory;
import com.r2coding.domain.strategy.service.raffle.IRaffleStrategy;
import com.r2coding.domain.strategy.service.rule.IRuleLogicFilter;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;

import javax.annotation.Resource;

/**
 * 执行抽奖 单元测试类
 *
 * @author caolx
 * @date 2025/7/22 23:19
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@SuppressWarnings("all")
public class RaffleStrategyTest {

    @Resource
    private IRaffleStrategy raffleStrategy;
    @Resource(name = "ruleWeightLogicFilter")
    private IRuleLogicFilter<RuleActionEntity.RaffleBeforeEntity> ruleWeightLogicFilter;
    @Resource(name = "ruleLockLogicFilter")
    private IRuleLogicFilter<RuleActionEntity.RaffleBeforeEntity> ruleLockLogicFilter;
    @Resource
    private IStrategyArmory strategyArmory;


    @Before
    public void setup() {
        strategyArmory.assembleLotteryStrategy(100002L);
        ReflectionTestUtils.setField(ruleWeightLogicFilter, "userScore", 0L);
        ReflectionTestUtils.setField(ruleLockLogicFilter, "userRaffleCount", 0L);
    }

    @Test
    public void test_performRaffle() {
        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("oyht")
                .strategyId(100001L)
                .build();
        RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
        log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
    }

    @Test
    public void test_performRaffle_whitelist() {
        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("caolixi")
                .strategyId(100001L)
                .build();
        RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
        log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
    }

    @Test
    public void test_performRaffle_blacklist() {
        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("user001")
                .strategyId(100001L)
                .build();
        RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
        log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
    }

    @Test
    public void test_perfromRaffle_center_rule_lock() {
        // 白名单用户，且抽奖次数校验
        RaffleFactorEntity raffleFactorEntity = RaffleFactorEntity.builder()
                .userId("caolixi")
                .strategyId(100002L)
                .build();

        RaffleAwardEntity raffleAwardEntity = raffleStrategy.performRaffle(raffleFactorEntity);
        log.info("测试结果：{}", JSON.toJSONString(raffleAwardEntity));
    }
}
